package com.yxsd.web.controller.admin;

import com.pandawork.core.exception.SSException;
import com.pandawork.core.log.LogClerk;
import com.pandawork.core.util.Pagination;
import com.yxsd.common.annotation.Module;
import com.yxsd.common.entity.Supplier;
import com.yxsd.common.enums.ModuleEnums;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AdminSupplierController
 *
 * @author: zhangteng
 * @time: 2015/1/4 19:46
 */
@Module(value = ModuleEnums.AdminSupplier)
@Controller
@RequestMapping(value = "admin/supplier")
public class AdminSupplierController extends AbstractController {

    @Module(value = ModuleEnums.AdminSupplierList)
    @RequestMapping(value = {"", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "search", required = false) String search,
                       Model model) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = supplierService.queryCountByCondition(search);
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Supplier> supplierList = supplierService.queryByCondition(page, search);

            model.addAttribute("supplierList", supplierList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "admin/supplier/list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @Module(value = ModuleEnums.AdminSupplierNew)
    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String toInsert() {
        return "admin/supplier/new";
    }
    @Module(value = ModuleEnums.AdminSupplierNew)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String insert(Supplier supplier) {
        try {
            supplierService.insert(supplier);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/supplier";
    }

    @Module(value = ModuleEnums.AdminSupplierEdit)
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id,
                         Model model) {
        try {
            Supplier supplier = supplierService.queryById(Supplier.class, id);
            model.addAttribute("supplier", supplier);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "admin/supplier/edit";
    }
    @Module(value = ModuleEnums.AdminSupplierEdit)
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public String update(Supplier supplier) {
        try {
            supplierService.update(supplier);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/supplier";
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    public @ResponseBody JSONObject del(@RequestParam("id") Integer id) {
        try {
            Supplier supplier = new Supplier();
            supplier.setId(id);
            supplierService.delete(supplier);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsgAndErrCode(e);
        }
        return sendSuccessMsg();
    }
}
